import sys
import caffe
import numpy as np
import os.path

if len(sys.argv) != 2:
    print "Usage: python convertProPy modelName{such as oxfordCatsAndDogs}"
    sys.exit()

modelName=sys.argv[1]
modelSourceDir='../data/' + modelName
modelNameSourceProto=modelName + '_mean.binaryproto'
modelNamePythonProto=modelName + '_mean.npy'
modelNamePythonDir='../python/caffe/' + modelName

if not os.path.isdir(modelNamePythonDir):
   print modelNamePythonDir + " is not a valid dir" 
   os.mkdir(modelNamePythonDir)

blob = caffe.proto.caffe_pb2.BlobProto()
data = open(modelSourceDir + '/' + modelNameSourceProto, "rb").read()
blob.ParseFromString(data)
nparray = np.array( caffe.io.blobproto_to_array(blob) )
f = file(modelNamePythonDir + '/' + modelNamePythonProto,"wb")
np.save(f,nparray[0])
f.close()
